// 注册按钮
$('.zhuce_er').click(function () {
    display('none', 'block', 'none')
    $('.verification').attr('src', "http://1.14.68.137:8000/image_code/?uuid=" + uuid());
})

// 返回登录按钮
$('.return_to_login').click(function () {
    $('.verification').attr('src', "http://1.14.68.137:8000/image_code/?uuid=" + uuid());
    display('block', 'none', 'none')
})

// 忘记密码按钮
$('.forget_password').click(function () {
    display('none', 'none', 'block')
})

// $('.verification').attr('src', "http://1.14.68.137:8000/image_code/?uuid=" + uuid());
var uuid_ = uuid()
$('.verification').attr('src', "http://1.14.68.137:8000/image_code/?uuid=" + uuid_);
$('.verification').click(function () {
    uuid_ = uuid()
    $('.verification').attr('src', "http://1.14.68.137:8000/image_code/?uuid=" + uuid_);
})
// $('.username_regexp').blur(function () {
//     regexp('.username_regexp');
//     $.post('http://1.14.68.137:8000/verify_username/', {
//         username: $('.username_regexp').val()
//     }, function (data) {
//         if (data.code == -1) {
//             $('.user_format').append($('<p style="color:red;font-size:14px">' + data.msg + '</p>'));
//         }
//     })
// })
//判断用户名可用
$('.username_regexp').blur(function () {
    var length = $('.username_regexp').val().length
    if (6 < length && length < 16) {
        $.post('http://1.14.68.137:8000/verify_username/', {
            username: $('.username_regexp').val()
        }, function (data) {
            $('.username_regexp').addClass('bor')
        })
    } else {
        alert('请输入6-16位之间的用户名')
        $('.username_regexp').val() = ''
    }
})

$('.username_regexp').blur(function () {
    var length = $(this).val().lenght
    if (6 < length < 16) {
        $(this).addClass('bor')
    }
})
$('.username_regexp').focus(function () {
    move('.user_format>p');
})

$('.password_regexp').focus(function () {
    move('.pass_format>p');
})

$('.password_regexp').blur(function () {
    regexp('', '.password_regexp');
})
$('.confirm_password').focus(function () {
    move('.conf_format>p')
    $('.confirm_password').val()
})

$('.confirm_password').blur(function () {
    if ($('.password_regexp').val() !== $('.confirm_password').val()) {
        $('.conf_format').append($('<p style="color:red;font-size:14px">密码格式错误</p>'));
    }
})
$('.mailbox_regexp').focus(function () {
    move('.mail_format>p');
})

var cookie = new Cookie();
$('.mailbox_regexp').blur(function () {
    regexp("", "", '.mailbox_regexp');
    $.post('http://1.14.68.137:8000/email/', {
        email: $('.mailbox_regexp').val()
    }, function (data) {
        cookie.set('name', data.no, 3);
        console.log(cookie.get('name'))
    })
})
// $('.btn_er').click(function () {
//     $.post('http://1.14.68.137:8000/register/', {
//         username: $('.username_regexp').val(),
//         password: $('.password_regexp').val(),
//         no: cookie.get('name'),
//         text: $('.code').val()
//     }, function (data) {
//         if (data.code == 0) {
//             $('.verification').attr('src', "http://1.14.68.137:8000/image_code/?uuid=" + uuid());
//             display('block', 'none', 'none')
//         } else {
//             $('.verif').append($('<p style="color:red;font-size:14px">' + data.msg + '</p>'))
//         }
//     })
//     console.log($('.code').val());
// })
//注册
$('.btn_er').click(function () {
    $.post('http://1.14.68.137:8000/register/', {
        username: $('.username_regexp').val(),
        password: $('.password_regexp').val(),
        no: cookie.get('name'),
        text: $('.code').val()
    }, function (data) {
        console.log(data);
            alert(data.msg)
            if (data.code==0) { 
                $('.biaodan').show()
            }
    })
})


$('.send_code').click(function () {
    clearInterval(itme)
    var second = 60;
    var itme = setInterval(function () {
        $('.send_code').html(second + 's')
        second--;
        if (second == 0) {
            clearInterval(itme)
            $('.send_code').html('发送验证码')
        }
    }, 1000)
})
//登陆
$('.btn').click(function () {
    $.post('http://1.14.68.137:8000/login/', {
        username: $('.log_user').val(),
        password: $('.log_pass').val(),
        uuid: uuid_,
        code: $('.log_ver').val()
    }, function (data) {
        console.log(data)
        if (data.code == 0) {
            cookie.set('token', data.token, 3)
            window.location.href = '../html/receAddress.html'
        } else {
            alert(data.msg)
        }
    })
})


//正则函数
function regexp(element, password, postbox) {
    var regexp2 = /^[A-z][A-z0-9|.]{5,11}/;
    var regexp3 = /^[A-z0-9][A-z0-9|_|-|.|+|*|/|-|@|#|$|%|&]{5,}/;
    var regexp4 = /[A-z0-9_-]+(\.[A-z0-9_-]+)+/;
    if ($(element).val()) {
        if (!regexp2.test($(element).val())) {
            $('.user_format').append($('<p style="color:red;font-size:14px">用户名格式错误</p>'));
        }
    }
    if ($(password).val()) {
        if (!regexp3.test($(password).val())) {
            $('.pass_format').append($('<p style="color:red;font-size:14px">密码格式错误</p>'));
        }
    }
    if ($(postbox).val()) {
        if (!regexp4.test($(postbox).val())) {
            $('.mail_format').append($('<p style="color:red;font-size:14px">邮箱格式错误</p>'))
        }
    }
}
//uuid函数
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";
    var uuid = s.join("");
    return uuid;
}
function display(sign_state, enroll_state, retrieve_state) {
    $('.non').css({
        display: sign_state
    });
    $('.non_er').css({
        display: enroll_state
    });
    $('.non_three').css({
        display: retrieve_state
    });
}
function move(remove) {
    //删除正则验证没通过后的p元素
    $(remove).remove();
}